﻿using Abp.Application.Services;
using Abp.Domain.Repositories;
using Batman.Cms.Core.Models.Mvc;
using Batman.Cms.Domains;
using Batman.Cms.Services.Applications.Inputs;
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace Batman.Cms.Services.Applications
{
    public class ApplicationManager : ApplicationService
    {
        public IRepository<Application> applicationRepository { get; set; }

        /// <summary>
        /// 新建应用
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResponseBase<Application> Create(CreateApplicationInput model)
        {
            var isExists = applicationRepository.GetAll().AsNoTracking().Where(t => !t.IsDeleted).Where(t => t.ApplicationName == model.ApplicationName).Any();
            if (isExists)
                return new ResponseBase<Application>(false, $"名为‘{model.ApplicationName}’的应用已存在");

            var entity = ObjectMapper.Map<Application>(model);
            entity = applicationRepository.Insert(entity);

            return new ResponseBase<Application>(true) { Result = entity };
        }
    }
}
